package com.printeverywhere.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.hadoop.conf.Configuration;
import org.apache.log4j.Logger;
import org.apache.nutch.searcher.Hit;
import org.apache.nutch.searcher.HitDetails;
import org.apache.nutch.searcher.Hits;
import org.apache.nutch.searcher.NutchBean;
import org.apache.nutch.searcher.Query;
import org.apache.nutch.util.NutchConfiguration;

import com.printeverywhere.dto.NutchSearch;
import com.printeverywhere.util.NutchUtil;

public class NutchSearchAction extends FrontBaseSupport {
	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private String nutchSearchString;
	private int maxHits = 1000;
	private NutchSearch nutchSearch = new NutchSearch();
	private List<NutchSearch> resultsList = new ArrayList<NutchSearch>();
	private Logger logger = Logger.getLogger(NutchSearchAction.class);
	public NutchSearchAction() {
		super(true, false);
	}

	public String execute() throws Exception {
		try{
			// String array which contains name of urls.
//			fileName Where it is to be written to url.txt
//			NutchUtil.writeToFile(urlName, fileName)
			Configuration nutchConf = NutchConfiguration.create();
			NutchBean nutchBean = new NutchBean(nutchConf);
			Query nutchQuery = Query.parse(nutchSearchString, nutchConf);
			Hits nutchHits = nutchBean.search(nutchQuery, maxHits);
			logger.info("Found " + nutchHits.getLength() + " hits\n");

			for (int i = 0; i < nutchHits.getLength(); i++) {
				Hit hit = nutchHits.getHit(i);
				HitDetails details = nutchBean.getDetails(hit);
				NutchSearch nutchSearch = new NutchSearch();
				nutchSearch.setTitle(details.getValue("title"));
				nutchSearch.setUrl(details.getValue("url"));
				nutchSearch.setSummary(nutchBean.getSummary(details, nutchQuery)
						.toString());
				logger.info("Title is: " + nutchSearch.getTitle());
				logger.info("(" + nutchSearch.getUrl() + ")");
				nutchSearch.setDate(new java.util.Date(nutchBean
						.getFetchDate(details)));
				logger.info("Date Fetched: " + nutchSearch.getDate());
				logger.info(nutchSearch.getSummary() + "\n");
				logger.info("----------------------------------------");
				resultsList.add(nutchSearch);
				logger.info("=====>  " +resultsList.get(i).getUrl());
			}
		}catch (Exception e) {
			addActionError("Custom Search not completed.");
			return INPUT;
		}
			logger.info("1");

		return SUCCESS;
	}

	public String getNutchSearchString() {
		return nutchSearchString;
	}

	public void setNutchSearchString(String nutchSearchString) {
		this.nutchSearchString = nutchSearchString;
	}

	public int getMaxHits() {
		return maxHits;
	}

	public void setMaxHits(int maxHits) {
		this.maxHits = maxHits;
	}

	public List<NutchSearch> getResultsList() {
		return resultsList;
	}

	public void setResultsList(List<NutchSearch> resultsList) {
		this.resultsList = resultsList;
	}
}
